﻿using Photon.SocketServer;
using System.Collections.Generic;
using Common;
using System.Xml.Serialization;
using System.IO;
namespace MyGameServer.Handler
{
    public class SyncPlayerHandler : BaseHandler
    {
        public SyncPlayerHandler() 
        {
            OpCode = OperationCode.SyncPlayer;
        }

        public override void OnOPerationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
            //取得所有已经登录的在线玩家//不包含当前客户端
             List<string> usernameList = new List<string>();  //改XML和json都可以,这种集合类数据都不支持，只支持int floa和string

            foreach (ClientPeer tempPeer in MyGameServer.Instance.peerList)
            {
                if (string.IsNullOrEmpty(tempPeer.userName)==false&& tempPeer!=peer)//其他客户端
                {
                    usernameList.Add(tempPeer.userName);
                }  ;
            }
            //将usernameList这个字典序列划成字夫窜
            StringWriter sw = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(List<string>));
            serializer.Serialize(sw, usernameList);
            sw.Close();
            string usernameListString = sw.ToString();
            Dictionary<byte, object> data = new Dictionary<byte, object>();
            data.Add((byte)ParameterCode.UsernameList, usernameListString);
            OperationResponse response = new OperationResponse(operationRequest.OperationCode);
            response.Parameters = data;
            peer.SendOperationResponse(response, sendParameters);
        }
    }
}
